package com.spring.spring.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring.spring.common.QueryPageParam;
import com.spring.spring.common.Result;
import com.spring.spring.entity.User;
import com.spring.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.invoke.LambdaConversionException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZT
 * @since 2023-11-09
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
//获取用户表信息
    @GetMapping("/list")
    public List<User> list(){
        return userService.list();
    }
//添加
    @PostMapping("/save")
    public boolean save(@RequestBody User user)
    {
        return userService.save(user);
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user)
    {
        return userService.updateById(user);
    }
    //更新和修改
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody User user)
    {
        return userService.saveOrUpdate(user);
    }
    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id)
    {
        return userService.removeById(id);
    }
    //模糊查询和匹配
    @PostMapping("/listP")
    public List<User> listP(@RequestBody User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(User::getName,user.getName());
        return userService.list(lambdaQueryWrapper);
    }


    //分页
    @PostMapping("/listPage")
    //public List<User> listPage(@RequestBody HashMap map){
    public List<User> listPage(@RequestBody QueryPageParam query){
        //System.out.println(query);

        //System.out.println("num==="+(String)map.get("pageSize"));

//        System.out.println("num==="+query.getPageNum());
//        System.out.println("size==="+query.getPageSize());

        HashMap param = query.getParam();
        String name = (String) param.get("name");
        System.out.println("null==="+(String)param.get("name"));
        System.out.println("no==="+(String)param.get("no"));

//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
//        lambdaQueryWrapper.like(User::getName,user.getName());

//        return userService.list(lambdaQueryWrapper);


        // 统计记入条数
        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(User::getName,name);

        IPage result = userService.page(page,lambdaQueryWrapper);

        System.out.println("totle=="+result.getTotal());

        return result.getRecords();
    }



      @PostMapping("/listPageC")
   //public List<User> listPage(@RequestBody HashMap map){
      public Result listPageC(@RequestBody QueryPageParam query){
        //System.out.println(query);

        //System.out.println("num==="+(String)map.get("pageSize"));

//        System.out.println("num==="+query.getPageNum());
//        System.out.println("size==="+query.getPageSize());

        HashMap param = query.getParam();
        String name = (String) param.get("name");
        System.out.println("null==="+(String)param.get("name"));
        System.out.println("no==="+(String)param.get("no"));

//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
//        lambdaQueryWrapper.like(User::getName,user.getName());

//        return userService.list(lambdaQueryWrapper);

        Page<User> page = new Page(1,2);
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
//        lambdaQueryWrapper.like(User::getName,name);

        IPage result = userService.pageC(page);

        System.out.println("totle=="+result.getTotal());

        return Result.suc(result.getRecords(),result.getTotal());

//          自定义条件wrapper
//          LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
//          lambdaQueryWrapper.like(User::getName,name);
//          IPage result = userService.pageCC(page,lambdaQueryWrapper);
//          System.out.println("totle=="+result.getTotal());
//          return result.getRecords();

    }

}
